#line 1 "X:/Electronic/easypic6/06-Temperature Display/Sam/EasyPIC6 Temperature Control.c"
#line 27 "X:/Electronic/easypic6/06-Temperature Display/Sam/EasyPIC6 Temperature Control.c"
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


const unsigned short TEMP_RESOLUTION = 9;
unsigned short Set_temp=30;
char *text2 = "000.00";
char *text1 = "Enter set temp";
char *St_text = "000.00";
char *Output_text;
unsigned temp;
bit oldstate1, oldstate2;

void InitMain() {
 ANSEL = 0;
 ANSELH = 0;
 C1ON_bit = 0;
 C2ON_bit = 0;
 Lcd_Init();
 LCD_Cmd(_LCD_CURSOR_OFF);
 LCD_Cmd(_LCD_CLEAR);

 TRISE0_bit = 1;
 TRISE1_bit = 1;
 oldstate1 = 0;
 oldstate2 = 0;


 T1CON = 1;
 TMR1IF_bit = 0;
 TMR1H = 0x85;
 TMR1L = 0xEE;
 TMR1IE_bit = 1;
 INTCON = 0xC0;
}

void Check_DeltaT() {
 signed char DeltaT;
 unsigned int temp2write;

 DeltaT = (Set_temp*2-temp2write);
 if (DeltaT > 0) {
 Output_text = "ON ";
 }
 else
 Output_text = "OFF";
 LCD_Out(2,1,Output_text);
}

void Display_Value(unsigned short s) {
 unsigned char ch_temp;
 ch_temp = s;


 if (ch_temp/100)
 St_text[0] = ch_temp/100 + 48;
 else
 St_text[0] = '0';
 St_text[1] = (ch_temp/10)%10 + 48;
 St_text[2] = ch_temp%10 + 48;
 LCD_Out(1,8,St_text);
}

void Check_Button() {
 if (RA0_bit) {
 Delay_ms(40);
 if (Set_temp < 85) {
 Set_temp++;
 }
 else {}


 Display_Value(Set_temp);
 }

 if (RA1_bit) {
 Delay_ms(40);
 if (Set_temp > 0) {
 Set_temp--;
 }
 else {}

 Display_Value(Set_temp);
 }
}

void Display_Temperature(unsigned int temp2write) {
 const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
 char temp_whole;
 unsigned int temp_fraction;

 if (temp2write & 0x8000) {
 text2[0] = '-';
 temp2write = ~temp2write + 1;
 }


 temp_whole = temp2write >> RES_SHIFT ;


 if (temp_whole/100)
 text2[0] = temp_whole/100 + 48;
 else
 text2[0] = '0';
 text2[1] = (temp_whole/10)%10 + 48;
 text2[2] = temp_whole%10 + 48;


 temp_fraction = temp2write << (4-RES_SHIFT);
 temp_fraction &= 0x000F;
 temp_fraction *= 625;


 text2[4] = temp_fraction/1000 + 48;
 text2[5] = (temp_fraction/100)%10 + 48;


 Lcd_Out(2, 8, text2);
}

void main() {
 InitMain();

 text1 = "Set:";
 LCD_Out(1,1,text1);


 Lcd_Chr(2,15,223);
 Lcd_Chr_Cp('C');
 Lcd_Chr(1,15,223);
 Lcd_Chr_Cp('C');

 do {

 Display_Value(Set_temp);
 Check_Button();
 Check_DeltaT();

 Ow_Reset(&PORTE, 2);
 Ow_Write(&PORTE, 2, 0xCC);
 Ow_Write(&PORTE, 2, 0x44);
 Delay_us(120);

 Ow_Reset(&PORTE, 2);
 Ow_Write(&PORTE, 2, 0xCC);
 Ow_Write(&PORTE, 2, 0xBE);

 temp = Ow_Read(&PORTE, 2);
 temp = (Ow_Read(&PORTE, 2) << 8) + temp;


 Display_Temperature(temp);
 Delay_ms(100);
 } while (1);
}
